package editor.windows;

import adventure.commands.skills.SkillManager;
import adventure.placeable.PlaceableManager;
import adventure.placeable.PlaceableManagerEdit;
import adventure.placeable.trigger.Trigger;
import adventure.placeable.trigger.conditions.TriggerCondition;
import adventure.placeable.trigger.TriggerEdit;
import adventure.placeable.trigger.results.TriggerResult;
import java.text.NumberFormat;

/**
 *
 * @author Michael Hanns
 *
 */
public class EditorTriggerView extends javax.swing.JDialog {

    private PlaceableManager placeables;
    private SkillManager skills;
    private int totalTriggers;
    private WITNAFrameView parent;

    /**
     * Creates new form EditorItemView
     */
    public EditorTriggerView(java.awt.Frame parent, PlaceableManager p, SkillManager s) {
        super(parent, true);
        this.parent = (WITNAFrameView) parent;
        placeables = p;
        skills = s;
        initComponents();
        if (placeables.getTrigger(1).getID() == 0) {
            ((PlaceableManagerEdit) placeables).newTrigger();
        }
        fillTable();
        getTriggerData(totalTriggers - 1);
    }

    public void fillTable() {
        triggerListTable.removeAll();

        int x = 1;
        while (placeables.getTrigger(x).getID() != 0) {
            triggerListTable.getModel().setValueAt(placeables.getTrigger(x).getID(), x - 1, 0);
            triggerListTable.getModel().setValueAt(((TriggerEdit) placeables.getTrigger(x)).getStandardName(), x - 1, 1);
            x++;
        }

        totalTriggers = x;
        triggerListTable.changeSelection(x - 1, 2, false, false);
    }

    public void getTriggerData(int row) {
        if (row > 0) {
            TriggerEdit trigger = (TriggerEdit) placeables.getTrigger(row);

            idBox.setText("" + trigger.getID());
            nameBox.setText(trigger.getStandardName());
            triggeredNameBox.setText(trigger.getTriggeredName());
            descriptionBox.setText(trigger.getDescription());

            visibleBox.setSelected(trigger.isVisible());
            lightSourceBox.setSelected(trigger.isLightSource());

            resultsList.clearSelection();

            limitedUsesCheckBox.setSelected(trigger.limitedUses());
            if (trigger.limitedUses()) {
                usesField.setText(trigger.getTotalUses() + "");
                usesField.setEnabled(true);
                triggeredNameBox.setEnabled(true);
            } else {
                usesField.setText("0");
                usesField.setEnabled(false);
                triggeredNameBox.setEnabled(false);
                triggeredNameBox.setText(trigger.getTriggeredName());
            }


            TriggerResult[] results = trigger.getResults();

            javax.swing.DefaultListModel model = new javax.swing.DefaultListModel();
            String modelText = "";
            for (int x = 0; x < results.length; x++) {
                modelText = results[x].getDescription();
                model.addElement(modelText);
            }
            resultsList.setModel(model);

            conditionsList.setModel(new javax.swing.DefaultListModel());
        }
    }

    public void getConditionData(int result) {
        if (result > -1) {
            TriggerEdit t = ((TriggerEdit) placeables.getTrigger(parseInt(idBox.getText())));
            TriggerCondition[] conditions = t.getResults()[result].getConditions();
            javax.swing.DefaultListModel model = new javax.swing.DefaultListModel();
            String modelText = "";
            for (int x = 0; x < conditions.length; x++) {
                modelText = conditions[x].getDescription();
                model.addElement(modelText);
            }
            conditionsList.setModel(model);
        } else {
            conditionsList.setModel(new javax.swing.DefaultListModel());
        }
    }

    private void addResultTo(int id) {
        applyChanges(false);
        TriggerEdit t = (TriggerEdit) placeables.getTrigger(id);
        DialogAddTriggerResultView dialog = new DialogAddTriggerResultView(parent, t, placeables, skills);
        final int selectedResult = resultsList.getSelectedIndex();
        final int selectedCondition = conditionsList.getSelectedIndex();
        final int totalResults = resultsList.getModel().getSize();
        dialog.addWindowListener(new java.awt.event.WindowAdapter() {
            @Override
            public void windowDeactivated(java.awt.event.WindowEvent e) {
                getTriggerData(parseInt(idBox.getText()));
                if (resultsList.getModel().getSize() > totalResults) {
                    resultsList.setSelectedIndex(resultsList.getModel().getSize() - 1);
                } else {
                    resultsList.setSelectedIndex(selectedResult);
                    conditionsList.setSelectedIndex(selectedCondition);
                }
            }
        });
        dialog.setVisible(true);
    }

    private void removeResultFrom(int id, int resultSelection) {
        applyChanges(false);
        TriggerEdit t = (TriggerEdit) placeables.getTrigger(id);
        t.removeResultAtIndex(resultSelection);
        ((PlaceableManagerEdit) placeables).editTrigger((Trigger) t);
        getTriggerData(t.getID());
    }

    private void addConditionTo(int id, int resultSelection) {
        TriggerEdit t = (TriggerEdit) placeables.getTrigger(id);
        applyChanges(false);
        TriggerResult tr = t.getResults()[resultsList.getSelectedIndex()];
        final int selectedResult = resultsList.getSelectedIndex();
        final int selectedCondition = conditionsList.getSelectedIndex();
        final int totalConditions = conditionsList.getModel().getSize();
        DialogAddTriggerConditionView dialog = new DialogAddTriggerConditionView(parent, t, tr, resultSelection, placeables);
        dialog.addWindowListener(new java.awt.event.WindowAdapter() {
            @Override
            public void windowDeactivated(java.awt.event.WindowEvent e) {
                getTriggerData(parseInt(idBox.getText()));
                if (conditionsList.getModel().getSize() > totalConditions) {
                    resultsList.setSelectedIndex(selectedResult);
                    conditionsList.setSelectedIndex(conditionsList.getModel().getSize() - 1);
                } else {
                    resultsList.setSelectedIndex(selectedResult);
                    conditionsList.setSelectedIndex(selectedCondition);
                }
            }
        });
        dialog.setVisible(true);
    }

    private void removeConditionFrom(int id, int resultSelection, int conditionSelection) {
        applyChanges(false);
        if (resultSelection > -1) {
            TriggerEdit t = (TriggerEdit) placeables.getTrigger(id);
            TriggerResult tr = t.getResults()[resultSelection];
            if (conditionSelection > -1) {
                tr.removeConditionAtIndex(conditionSelection);
                ((PlaceableManagerEdit) placeables).editTrigger((Trigger) t);
                getTriggerData(t.getID());
            } else {
                parent.printToConsole("You must select a condition before attempting to remove it.");
            }
        } else {
            parent.printToConsole("You must select a result before attempting to remove a condition.");
        }
    }

    private void editResult(final int id, final int resultSelection) {
        applyChanges(false);
        TriggerEdit t = (TriggerEdit) placeables.getTrigger(id);
        TriggerResult tr = t.getResults()[resultSelection];

        final int selectedCondition = conditionsList.getSelectedIndex();
        DialogEditTriggerResultView dialog = new DialogEditTriggerResultView(parent, t, tr, resultSelection, placeables, skills);
        dialog.addWindowListener(new java.awt.event.WindowAdapter() {
            @Override
            public void windowDeactivated(java.awt.event.WindowEvent e) {
                getTriggerData(id);
                resultsList.setSelectedIndex(resultSelection);
                conditionsList.setSelectedIndex(selectedCondition);
            }
        });
        dialog.setVisible(true);
    }

    private void moveResult(int id, int resultSelection, boolean up) {
        TriggerEdit trigger = ((TriggerEdit) placeables.getTrigger(id));

        if (up) {
            trigger.moveResultUp(resultSelection);
            resultSelection--;
        } else {
            trigger.moveResultDown(resultSelection);
            resultSelection++;
        }

        ((PlaceableManagerEdit) placeables).editTrigger((Trigger) trigger);

        getTriggerData(parseInt(idBox.getText()));
        parent.printToConsole("Trigger " + trigger.getStandardName() + " results reordered.");
    }

    private void editCondition(final int id, final int resultSelection, final int conditionSelection) {
        applyChanges(false);
        TriggerEdit t = (TriggerEdit) placeables.getTrigger(id);
        TriggerResult tr = t.getResults()[resultSelection];
        DialogEditTriggerConditionView dialog = new DialogEditTriggerConditionView(
                parent, t, tr, resultSelection, conditionSelection, placeables);

        dialog.addWindowListener(new java.awt.event.WindowAdapter() {
            @Override
            public void windowDeactivated(java.awt.event.WindowEvent e) {
                getTriggerData(parseInt(idBox.getText()));
                resultsList.setSelectedIndex(resultSelection);
                conditionsList.setSelectedIndex(conditionSelection);
            }
        });
        dialog.setVisible(true);
    }

    private void moveCondition(int id, int resultSelection, int conditionSelection, boolean up) {
        TriggerEdit trigger = ((TriggerEdit) placeables.getTrigger(id));

        if (up) {
            trigger.moveConditionUp(resultSelection, conditionSelection);
        } else {
            trigger.moveConditionDown(resultSelection, conditionSelection);
        }

        ((PlaceableManagerEdit) placeables).editTrigger((Trigger) trigger);

        getConditionData(conditionSelection);
        parent.printToConsole("Trigger " + trigger.getStandardName() + " conditions reordered.");
    }

    private void newTrigger() {
        if (totalTriggers <= 200) {
            if (placeables.getTrigger(totalTriggers - 1).getName().trim().length() == 0) {
                parent.printToConsole("You must specify a name for all existing triggers before creating a new one!");
            } else {
                ((PlaceableManagerEdit) placeables).newTrigger();
                fillTable();
                getTriggerData(totalTriggers - 1);
            }
        } else {
            parent.printToConsole("You have reached the maximum number of triggers!");
        }
    }

    private void duplicateTrigger(int id) {
        if (totalTriggers <= 200) {
            if (((TriggerEdit) placeables.getTrigger(totalTriggers - 1)).getStandardName().trim().length() == 0) {
                parent.printToConsole("You must specify a name for all existing triggers before duplicating one!");
            } else {
                if (id > 0) {
                    System.out.println("id > 0");
                    if (placeables.getTrigger(id).getID() != 0) {
                        TriggerEdit newTrigger = (TriggerEdit) placeables.getTrigger(id).cloneThis();
                        newTrigger.setID(totalTriggers);
                        ((PlaceableManagerEdit) placeables).newTrigger(newTrigger);
                        fillTable();
                        getTriggerData(totalTriggers - 1);
                    } else {
                        parent.printToConsole("You must select an existing trigger before attempting to duplicate!");
                    }
                }
            }
        } else {
            parent.printToConsole("You have reached the maximum number of trigger!");
        }
    }

    private void applyChanges(boolean refresh) {
        int id = parseInt(idBox.getText());
        String name = nameBox.getText();

        if (id > 0 && name.length() > 0) {
            String triggeredName = triggeredNameBox.getText();
            String description = descriptionBox.getText();

            boolean lightSource = lightSourceBox.isSelected();
            boolean visible = visibleBox.isSelected();

            TriggerEdit newT = (TriggerEdit) placeables.getTrigger(id);
            newT.setName(name);
            newT.setTriggeredName(triggeredName);
            newT.setDescription(description);
            newT.setLightSource(lightSource);
            newT.setVisible(visible);
            newT.setLimitedUses(limitedUsesCheckBox.isSelected());
            if (limitedUsesCheckBox.isSelected()) {
                newT.setTotalUses(parseInt(usesField.getText()));
            } else {
                newT.setTotalUses(0);
            }

            ((PlaceableManagerEdit) placeables).editTrigger((Trigger) newT);
            if (refresh) {
                fillTable();
            }
            parent.setUnsavedChanges(true);

            parent.printToConsole("Trigger " + name + " updated.");
        } else {
            parent.printToConsole("WARNING: Trigger changes not saved. You must specify a name for the trigger!");
        }
    }

    private int parseInt(String text) {
        try {
            int i = Integer.parseInt(text);
            return i;
        } catch (NumberFormatException e) {
            return 0;
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane2 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        itemListPane = new javax.swing.JScrollPane();
        triggerListTable = new javax.swing.JTable();
        nameBox = new javax.swing.JFormattedTextField();
        idBox = new javax.swing.JFormattedTextField(NumberFormat.getInstance());
        descriptionScrollPane = new javax.swing.JScrollPane();
        descriptionBox = new javax.swing.JTextPane();
        idLabel = new javax.swing.JLabel();
        nameLabel = new javax.swing.JLabel();
        descriptionLabel = new javax.swing.JLabel();
        separator = new javax.swing.JSeparator();
        closeButton = new javax.swing.JButton();
        newTriggerButton = new javax.swing.JButton();
        applyButton = new javax.swing.JButton();
        triggeredNameBox = new javax.swing.JFormattedTextField();
        triggerNameLabel = new javax.swing.JLabel();
        resultsLabel = new javax.swing.JLabel();
        resultsScrollPane = new javax.swing.JScrollPane();
        resultsList = new javax.swing.JList();
        addResultButton = new javax.swing.JButton();
        removeResultButton = new javax.swing.JButton();
        conditionsLabel = new javax.swing.JLabel();
        conditionsScrollPane = new javax.swing.JScrollPane();
        conditionsList = new javax.swing.JList();
        removeConditionButton = new javax.swing.JButton();
        addConditionButton = new javax.swing.JButton();
        usesField = new javax.swing.JFormattedTextField(NumberFormat.getInstance());
        lightSourceBox = new javax.swing.JCheckBox();
        visibleBox = new javax.swing.JCheckBox();
        usesLabel = new javax.swing.JLabel();
        limitedUsesCheckBox = new javax.swing.JCheckBox();
        duplicateButton = new javax.swing.JButton();
        moveResultDownButton = new javax.swing.JButton();
        moveResultUpButton = new javax.swing.JButton();
        moveConditionUpButton = new javax.swing.JButton();
        moveConditionDownButton = new javax.swing.JButton();

        jList1.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane2.setViewportView(jList1);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Edit Triggers");
        setLocationByPlatform(true);
        setResizable(false);

        triggerListTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "#", "Name"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        triggerListTable.setToolTipText("Trigger list");
        triggerListTable.setColumnSelectionAllowed(true);
        triggerListTable.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        triggerListTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                triggerListTableMouseClicked(evt);
            }
        });
        itemListPane.setViewportView(triggerListTable);
        triggerListTable.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        triggerListTable.getColumnModel().getColumn(0).setMinWidth(10);
        triggerListTable.getColumnModel().getColumn(0).setPreferredWidth(25);
        triggerListTable.getColumnModel().getColumn(0).setMaxWidth(25);

        nameBox.setToolTipText("Trigger name");

        idBox.setEditable(false);
        idBox.setToolTipText("Trigger number");

        descriptionBox.setToolTipText("Trigger description");
        descriptionScrollPane.setViewportView(descriptionBox);

        idLabel.setText("ID:");

        nameLabel.setText("Name:");

        descriptionLabel.setText("Description:");

        closeButton.setText("Close");
        closeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                closeButtonActionPerformed(evt);
            }
        });

        newTriggerButton.setText("Add new..");
        newTriggerButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newTriggerButtonActionPerformed(evt);
            }
        });

        applyButton.setText("Apply");
        applyButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                applyButtonActionPerformed(evt);
            }
        });

        triggeredNameBox.setToolTipText("The 'triggered name' is the name of the trigger once it has expended its total uses.");

        triggerNameLabel.setText("Triggered name:");

        resultsLabel.setText("Results:");

        resultsScrollPane.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

        resultsList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        resultsList.setToolTipText("Double click to edit Result");
        resultsList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                resultsListMouseClicked(evt);
            }
        });
        resultsList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                resultsListValueChanged(evt);
            }
        });
        resultsList.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                resultsListKeyPressed(evt);
            }
        });
        resultsScrollPane.setViewportView(resultsList);

        addResultButton.setText("+");
        addResultButton.setMargin(new java.awt.Insets(2, 8, 2, 8));
        addResultButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addResultButtonActionPerformed(evt);
            }
        });

        removeResultButton.setText("-");
        removeResultButton.setMargin(new java.awt.Insets(2, 8, 2, 8));
        removeResultButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeResultButtonActionPerformed(evt);
            }
        });

        conditionsLabel.setText("Conditions:");

        conditionsScrollPane.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

        conditionsList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        conditionsList.setToolTipText("Double click to edit Condition");
        conditionsList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                conditionsListMouseClicked(evt);
            }
        });
        conditionsList.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                conditionsListKeyPressed(evt);
            }
        });
        conditionsScrollPane.setViewportView(conditionsList);

        removeConditionButton.setText("-");
        removeConditionButton.setMargin(new java.awt.Insets(2, 8, 2, 8));
        removeConditionButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeConditionButtonActionPerformed(evt);
            }
        });

        addConditionButton.setText("+");
        addConditionButton.setMargin(new java.awt.Insets(2, 8, 2, 8));
        addConditionButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addConditionButtonActionPerformed(evt);
            }
        });

        usesField.setText("0");

        lightSourceBox.setText("Light Source");

        visibleBox.setText("Visible");

        usesLabel.setText("Uses:");

        limitedUsesCheckBox.setText("Limited Uses");
        limitedUsesCheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                limitedUsesCheckBoxActionPerformed(evt);
            }
        });

        duplicateButton.setText("Duplicate");
        duplicateButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                duplicateButtonActionPerformed(evt);
            }
        });

        moveResultDownButton.setText("▼");
        moveResultDownButton.setMargin(new java.awt.Insets(2, 8, 2, 8));
        moveResultDownButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                moveResultDownButtonActionPerformed(evt);
            }
        });

        moveResultUpButton.setText("▲");
        moveResultUpButton.setMargin(new java.awt.Insets(2, 8, 2, 8));
        moveResultUpButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                moveResultUpButtonActionPerformed(evt);
            }
        });

        moveConditionUpButton.setText("▲");
        moveConditionUpButton.setMargin(new java.awt.Insets(2, 8, 2, 8));
        moveConditionUpButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                moveConditionUpButtonActionPerformed(evt);
            }
        });

        moveConditionDownButton.setText("▼");
        moveConditionDownButton.setMargin(new java.awt.Insets(2, 8, 2, 8));
        moveConditionDownButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                moveConditionDownButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(itemListPane, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(idBox, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(idLabel))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(nameLabel)
                                    .addComponent(nameBox)))
                            .addComponent(triggeredNameBox)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(triggerNameLabel)
                                    .addComponent(descriptionLabel))
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(0, 0, Short.MAX_VALUE)
                                        .addComponent(usesLabel)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(usesField, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(descriptionScrollPane)
                                        .addGap(6, 6, 6)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(lightSourceBox)
                                            .addComponent(visibleBox)
                                            .addComponent(limitedUsesCheckBox))))
                                .addGap(4, 4, 4))))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(separator))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(conditionsLabel)
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(conditionsScrollPane, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                        .addComponent(resultsLabel)
                                        .addGap(0, 0, Short.MAX_VALUE))
                                    .addComponent(resultsScrollPane, javax.swing.GroupLayout.Alignment.LEADING))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(moveResultUpButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(addResultButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(moveResultDownButton, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(removeResultButton, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(addConditionButton, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(removeConditionButton, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(moveConditionUpButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(moveConditionDownButton, javax.swing.GroupLayout.Alignment.TRAILING))))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(newTriggerButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(duplicateButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(closeButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(applyButton)))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(nameLabel)
                            .addComponent(idLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(nameBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(idBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(triggerNameLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(triggeredNameBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(descriptionLabel)
                        .addGap(7, 7, 7)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(lightSourceBox)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(visibleBox)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(limitedUsesCheckBox)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(usesField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(usesLabel)))
                            .addComponent(descriptionScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addComponent(separator, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(resultsLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(addResultButton)
                                .addGap(1, 1, 1)
                                .addComponent(removeResultButton, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(moveResultUpButton, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(1, 1, 1)
                                .addComponent(moveResultDownButton))
                            .addComponent(resultsScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(conditionsLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(addConditionButton)
                                .addGap(0, 0, 0)
                                .addComponent(removeConditionButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(moveConditionUpButton, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(1, 1, 1)
                                .addComponent(moveConditionDownButton))
                            .addComponent(conditionsScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                        .addGap(18, 18, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(applyButton)
                            .addComponent(newTriggerButton)
                            .addComponent(closeButton)
                            .addComponent(duplicateButton)))
                    .addComponent(itemListPane))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void triggerListTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_triggerListTableMouseClicked
        int row = triggerListTable.getSelectedRow();

        if (triggerListTable.getModel().getValueAt(row, 0) != null) {
            int id = parseInt(triggerListTable.getModel().getValueAt(row, 0).toString());
            getTriggerData(id);
        }
    }//GEN-LAST:event_triggerListTableMouseClicked

    private void closeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeButtonActionPerformed
        dispose();
    }//GEN-LAST:event_closeButtonActionPerformed

    private void resultsListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_resultsListValueChanged
        getConditionData(resultsList.getSelectedIndex());
    }//GEN-LAST:event_resultsListValueChanged

    private void applyButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_applyButtonActionPerformed
        applyChanges(true);
}//GEN-LAST:event_applyButtonActionPerformed

    private void newTriggerButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newTriggerButtonActionPerformed
        newTrigger();
}//GEN-LAST:event_newTriggerButtonActionPerformed

    private void addResultButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addResultButtonActionPerformed
        int id = parseInt(idBox.getText());

        addResultTo(id);
    }//GEN-LAST:event_addResultButtonActionPerformed

    private void removeResultButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeResultButtonActionPerformed
        int id = parseInt(idBox.getText());
        int result = resultsList.getSelectedIndex();
        removeResultFrom(id, result);
    }//GEN-LAST:event_removeResultButtonActionPerformed

    private void addConditionButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addConditionButtonActionPerformed
        int id = parseInt(idBox.getText());
        int resultSelection = resultsList.getSelectedIndex();

        addConditionTo(id, resultSelection);
    }//GEN-LAST:event_addConditionButtonActionPerformed

    private void removeConditionButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeConditionButtonActionPerformed
        int id = parseInt(idBox.getText());
        int resultSelection = resultsList.getSelectedIndex();
        int conditionSelection = conditionsList.getSelectedIndex();

        removeConditionFrom(id, resultSelection, conditionSelection);
    }//GEN-LAST:event_removeConditionButtonActionPerformed

    private void resultsListMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_resultsListMouseClicked
        if (evt.getClickCount() == 2 && resultsList.getSelectedIndex() > -1) {
            int id = parseInt(idBox.getText());
            int resultSelection = resultsList.getSelectedIndex();
            editResult(id, resultSelection);
        }
    }//GEN-LAST:event_resultsListMouseClicked

    private void resultsListKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_resultsListKeyPressed
        int id = parseInt(idBox.getText());
        int resultSelection = resultsList.getSelectedIndex();
        if (resultSelection > -1) {
            if (evt.getKeyCode() == 38 && resultSelection > 0) { // Up
                moveResult(id, resultSelection, true);
                resultsList.setSelectedIndex(resultSelection);
            } else if (evt.getKeyCode() == 40 && resultSelection < resultsList.getModel().getSize() - 1) { // Down
                moveResult(id, resultSelection, false);
                resultsList.setSelectedIndex(resultSelection);
            }
        }
    }//GEN-LAST:event_resultsListKeyPressed

    private void conditionsListKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_conditionsListKeyPressed
        int id = parseInt(idBox.getText());
        int resultSelection = resultsList.getSelectedIndex();
        int conditionSelection = conditionsList.getSelectedIndex();
        if (conditionSelection > -1) {
            if (evt.getKeyCode() == 38 && conditionSelection > 0) { // Up
                moveCondition(id, resultSelection, conditionSelection, true);
                conditionsList.setSelectedIndex(conditionSelection);
            } else if (evt.getKeyCode() == 40 && conditionSelection < conditionsList.getModel().getSize() - 1) { // Down
                moveCondition(id, resultSelection, conditionSelection, false);
                conditionsList.setSelectedIndex(conditionSelection);
            }
        }
    }//GEN-LAST:event_conditionsListKeyPressed

    private void conditionsListMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_conditionsListMouseClicked
        if (evt.getClickCount() == 2 && conditionsList.getSelectedIndex() > -1) {
            int id = parseInt(idBox.getText());
            int resultSelection = resultsList.getSelectedIndex();
            int conditionSelection = conditionsList.getSelectedIndex();

            editCondition(id, resultSelection, conditionSelection);
        }
    }//GEN-LAST:event_conditionsListMouseClicked

    private void limitedUsesCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_limitedUsesCheckBoxActionPerformed
        if (limitedUsesCheckBox.isSelected()) {
            usesField.setText("0");
            usesField.setEnabled(true);
            triggeredNameBox.setEnabled(true);
            triggeredNameBox.setText("triggered " + nameBox.getText());
        } else {
            usesField.setText("0");
            usesField.setEnabled(false);
        }
    }//GEN-LAST:event_limitedUsesCheckBoxActionPerformed

    private void duplicateButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_duplicateButtonActionPerformed
        int id = Integer.parseInt(idBox.getText());

        duplicateTrigger(id);
    }//GEN-LAST:event_duplicateButtonActionPerformed

    private void moveResultDownButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_moveResultDownButtonActionPerformed
        int id = parseInt(idBox.getText());
        int resultSelection = resultsList.getSelectedIndex();
        if (resultSelection > -1) {
            if (resultSelection < resultsList.getModel().getSize() - 1) { // Down
                moveResult(id, resultSelection, false);
                resultSelection++;
                resultsList.setSelectedIndex(resultSelection);
            }
        }
    }//GEN-LAST:event_moveResultDownButtonActionPerformed

    private void moveResultUpButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_moveResultUpButtonActionPerformed
        int id = parseInt(idBox.getText());
        int resultSelection = resultsList.getSelectedIndex();
        if (resultSelection > -1) {
            if (resultSelection > 0) { // Up
                moveResult(id, resultSelection, true);
                resultSelection--;
                resultsList.setSelectedIndex(resultSelection);
            }
        }
    }//GEN-LAST:event_moveResultUpButtonActionPerformed

    private void moveConditionUpButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_moveConditionUpButtonActionPerformed
        int id = parseInt(idBox.getText());
        int resultSelection = resultsList.getSelectedIndex();
        int conditionSelection = conditionsList.getSelectedIndex();
        if (conditionSelection > -1) {
            if (conditionSelection > 0) { // Up
                moveCondition(id, resultSelection, conditionSelection, true);
                conditionSelection--;
                conditionsList.setSelectedIndex(conditionSelection);
            }
        }
    }//GEN-LAST:event_moveConditionUpButtonActionPerformed

    private void moveConditionDownButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_moveConditionDownButtonActionPerformed
        int id = parseInt(idBox.getText());
        int resultSelection = resultsList.getSelectedIndex();
        int conditionSelection = conditionsList.getSelectedIndex();
        if (conditionSelection > -1) {
            if (conditionSelection < conditionsList.getModel().getSize() - 1) { // Down
                moveCondition(id, resultSelection, conditionSelection, false);
                conditionSelection++;
                conditionsList.setSelectedIndex(conditionSelection);
            }
        }
    }//GEN-LAST:event_moveConditionDownButtonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addConditionButton;
    private javax.swing.JButton addResultButton;
    private javax.swing.JButton applyButton;
    private javax.swing.JButton closeButton;
    private javax.swing.JLabel conditionsLabel;
    private javax.swing.JList conditionsList;
    private javax.swing.JScrollPane conditionsScrollPane;
    private javax.swing.JTextPane descriptionBox;
    private javax.swing.JLabel descriptionLabel;
    private javax.swing.JScrollPane descriptionScrollPane;
    private javax.swing.JButton duplicateButton;
    private javax.swing.JFormattedTextField idBox;
    private javax.swing.JLabel idLabel;
    private javax.swing.JScrollPane itemListPane;
    private javax.swing.JList jList1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JCheckBox lightSourceBox;
    private javax.swing.JCheckBox limitedUsesCheckBox;
    private javax.swing.JButton moveConditionDownButton;
    private javax.swing.JButton moveConditionUpButton;
    private javax.swing.JButton moveResultDownButton;
    private javax.swing.JButton moveResultUpButton;
    private javax.swing.JFormattedTextField nameBox;
    private javax.swing.JLabel nameLabel;
    private javax.swing.JButton newTriggerButton;
    private javax.swing.JButton removeConditionButton;
    private javax.swing.JButton removeResultButton;
    private javax.swing.JLabel resultsLabel;
    private javax.swing.JList resultsList;
    private javax.swing.JScrollPane resultsScrollPane;
    private javax.swing.JSeparator separator;
    private javax.swing.JTable triggerListTable;
    private javax.swing.JLabel triggerNameLabel;
    private javax.swing.JFormattedTextField triggeredNameBox;
    private javax.swing.JFormattedTextField usesField;
    private javax.swing.JLabel usesLabel;
    private javax.swing.JCheckBox visibleBox;
    // End of variables declaration//GEN-END:variables
}
